package za.co.yellowfire.solarflare.web.model;

import za.co.yellowfire.solarflare.annotation.Searchable;
import za.co.yellowfire.solarflare.annotation.SearchableProperty;
import za.co.yellowfire.solarflare.annotation.SearchablePropertyId;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
import java.util.Date;

/**
 * @author Mark P Ashworth
 * @version 0.0.1
 */
@Searchable(name = "Todo")
@Entity(name = "Todo")
@Access(AccessType.FIELD)
@Table(name = "todo", catalog = "slr")
@NamedQueries({
        @NamedQuery(
            name="qry.todos",
            query="select t from Todo t"
        )
})
@XmlType(name = "todo", propOrder = {"id", "description", "created", "due", "completed"})
public class Todo extends DomainEntity {

    private static final long serialVersionUID = 1L;

    public static final String QRY_TODOS = "qry.todos";

	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "todo_id", nullable = false, insertable = true, updatable = true)
    @XmlAttribute(name = "id", required = false)
    @SearchablePropertyId
    private Long id;

    @Column(name = "todo_description")
    @XmlAttribute(name = "description", required = false)
    @SearchableProperty
    private String description;

    @Column(name = "todo_created")
    @Temporal(TemporalType.TIMESTAMP)
    @XmlAttribute(name = "created", required = false)
    private Date created;

    @Column(name = "todo_due")
    @Temporal(TemporalType.TIMESTAMP)
    @XmlAttribute(name = "due", required = false)
    private Date due;

    @Column(name = "todo_completed")
    @Temporal(TemporalType.TIMESTAMP)
    @XmlAttribute(name = "completed", required = false)
    private Date completed;

    @Version
    @Column(name = "version")
    @SuppressWarnings("unused")
    private int version;

    /**
     * Default constructor
     */
    public Todo() { }

    /**
     * Creates the to do item
     * @param description The description of the to do
     * @param due The due date
     */
    public Todo(String description, Date due) {
        this.description = description;
        this.created = new Date();
        this.due = due;
    }

    /**
     * The persistent identity of the domain object
     *
     * @return The persistent identity
     */
    @Override
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getDue() {
        return due;
    }

    public void setDue(Date due) {
        this.due = due;
    }

    public Date getCompleted() {
        return completed;
    }

    public void setCompleted(Date completed) {
        this.completed = completed;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Todo)) return false;

        Todo todo = (Todo) o;

        if (id != null ? !id.equals(todo.id) : todo.id != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }

    @Override
    public String toString() {
        return "Todo {" +
                "id=" + id +
                ", description='" + description + '\'' +
                ", created=" + created +
                ", due=" + due +
                ", completed=" + completed +
                '}';
    }
}
